var wxNum;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    questions:[],
    /*question: "我要在第二行的时候多余隐藏我要在第二行的时候多余隐藏我要在第二行的时候多余隐藏我要在第二行的时候多余隐藏我要在第二行的时候多余隐藏我要在第二行的时候多余隐藏",*/
    noquestion:"none",
    showQue:"none",
    NoshowQUe:"display"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  test:function(e){
    var index=e.currentTarget.id;
    console.log(this.data.questions[index].creatDate)
  },
  question:function(){
    var that = this;
    wx.getStorage({
      key: 'login_uuId',
      success: function(res) {
        console.log('获取成功')
        
        getApp().globalData.openid= wxNum=res.data;
        wx.request({
          url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByUser",
          data: {
            wxNumber: wxNum
          },
          method: "GET",
          success: function (res) {
            console.log(res.data.length, '长度');
            var questions = res.data;
            console.log('数据', questions, '数据长度', questions.length);
            if (questions.length == 0) {

              that.setData(
                {
                  noquestion: "display",
                  NoshowQue: "none",
                  showQue: "none"

                }
              );
            }
            else {

              that.setData(
                {
                  noquestion: "none",
                  NoshowQue: "none",
                  showQue: "display"
                }
              );

              for (var i = 0; i < questions.length; i++) {
                
                questions[i].f4 =0;
                for(var k=0;k<questions[i].answerList.length;k++){
                  if(questions[i].answerList[k].isRead==0)
                  questions[i].f4++;
                }
                if (questions[i].f4 == 0) {
                  questions[i].f5 = "none"
                }
                else {
                  questions[i].f5 = "display"
                }
                for (var j = 0; j < questions[i].creatDate.length; j++) {
                  if (questions[i].creatDate[j] == ":")
                    break;
                }
                questions[i].creatDate = questions[i].creatDate.substring(j - 2, j + 3);
              }


              that.setData(
                {
                  questions: questions,

                }
              );
            }
            console.log(that.data.noquestion, that.data.NoshowQue, that.data.showQue)

          },
          fail: function () {
            console.log('请求失败');
          },

        })
      },
      fail:function(){
        that.tologin();
      },
    })
  
  },
  upData:function(){
    var that=this;
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
      data:{
        login_uuId:wxNum
      },
      method:'GET',
      success:function(res){
        if(res.data=='success')
        that.question();
        else{
          that.tologin();
        }

      },
    })
  },
  Login:function(){
    var that=this;
   console.log("重新登录")
    wx.checkSession({
      success:function(){
          wx.getStorage({
            key: 'login_uuId',
            success: function(res) {
              
              wxNum=res.data;
              console.log("这是我登录的时候的wxNumber"+wxNum);
              that.upData();
              //that.question();
            },
            fali:function(){
              that.tologin();
            },
          })
         
      },
      fail:function(){
        console.log("fail")
        that.tologin();
      },
      
    })
      

    //

  },

  tologin:function(){
    wx.showToast({
      title: '登录中',
      icon: 'loading'
    })
    var that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLogin',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log("return" + res.data);
              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });

             getApp().globalData.openid= wxNum = res.data;
             
              that.question();
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  lookAllNote:function(){
    wx.navigateTo({
      url: '../allNote/allNote?openid='+getApp().globalData.openid,
    })
  },
  onLoad: function (options) {
    console.log("ues")
   
  //this.Login();
    this.question();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.question();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})